package com.foxconn.ipebg.common.base.controller;

import ipebg.mzk.facade.system.service.DatasourceMonitorService;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.foxconn.ipebg.common.base.security.SecurityUserHolder;

@Controller
@RequestMapping("/datasource.do")
public class DatasourceController {
	@Resource(name = "datasourceMonitorServiceImpl")
	private DatasourceMonitorService datasourceMonitorServiceImpl;

	@RequestMapping(params = "action=getDatasourceInfo")
	public void getDatasourceInfo(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/xml");
		response.setCharacterEncoding("UTF-8");
		StringBuffer xmlDoc = new StringBuffer();
		xmlDoc.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
		/*xmlDoc.append("<connPool>");
		xmlDoc.append("<dataSource>");
		xmlDoc.append("<applyName>MZK-FW</applyName>");
		xmlDoc.append("<dataSourceName>MZK-DataSource</dataSourceName>");
		xmlDoc.append("<poolSize>" + datasourceMonitorServiceImpl.getDatasourcePoolSize() + "</poolSize>");// 连接池最大数
		xmlDoc.append("<poolUsed>" + datasourceMonitorServiceImpl.getDatasourcePoolActivesize() + "</poolUsed>");// 连接池的连接数
		xmlDoc.append("</dataSource>");
		xmlDoc.append("</connPool>");*/

		double hitCount = datasourceMonitorServiceImpl.getQueryCacheHitCount();
		double missCount = datasourceMonitorServiceImpl.getQueryCacheMissCount();
		double hitPercent = hitCount * 100 / (hitCount + missCount+1);
		xmlDoc.append("<cacheData>");
		xmlDoc.append("<cacheHitCount>");
		xmlDoc.append(hitCount);
		xmlDoc.append("</cacheHitCount>");

		xmlDoc.append("<cacheMissCount>");
		xmlDoc.append(missCount);
		xmlDoc.append("</cacheMissCount>");

		xmlDoc.append("<cacheHitPercent>");
		xmlDoc.append(hitPercent + "%");
		xmlDoc.append("</cacheHitPercent>");

		String[] regionNames = datasourceMonitorServiceImpl.getSecondLevelCacheRegionNames();
		
		if (regionNames != null) {
			for (int i = 0; i < regionNames.length; i++) {
				xmlDoc.append("<entities>");
				xmlDoc.append("<region-name>");
				xmlDoc.append(regionNames[i]);
				xmlDoc.append("</region-name>");
				xmlDoc.append("<region-cache-size>");
				try {
					Map map = datasourceMonitorServiceImpl.getSecondLevelCache(regionNames[i]);
					if (map != null) {
						xmlDoc.append(map.size());
					} else {
						xmlDoc.append("0");
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				xmlDoc.append("</region-cache-size>");
				xmlDoc.append("</entities>");
			}			
		}
		xmlDoc.append("</cacheData>");
		String rtnXml = xmlDoc.toString();
		System.out.println(rtnXml);

		try {
			response.getWriter().write(rtnXml);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
